<template>
  <div class="contact-edit">
    <header-tab>
      <span slot="left" @click="Go_back"><van-icon name="arrow-left" /></span>
      <span slot="title">编辑联系人</span>
    </header-tab>
    <van-contact-edit is-edit :contact-info="editingContact" @save="onSave" @delete="onDelete" />
  </div>
</template>

<script>
import { Toast } from 'vant';
import { Dialog } from 'vant';
import HeaderTab from '../components/HeaderTab.vue';
export default {
  components: { HeaderTab },
  props: ['manid'],
  data() {
    return { editingContact: {} };
  },
  methods: {
    onSave(contactInfo) {
      let url = `user/changelinkman`;
      // console.log(this.editingContact.name);
      let params = `id=${this.editingContact.id}&&name=${contactInfo.name}&&tel=${contactInfo.tel}`;
      this.axios.put(url, params).then(res => {
        console.log(res);
        if ((res.data.code = 200)) {
          Toast.success('修改成功');
          setTimeout(() => {
            this.$router.back();
          }, 2000);
        }
      });
    },
    onDelete(contactInfo) {
      // on confirm
      const url = 'user/deletelinkman';
      let manid = this.editingContact.id;
      let params = `manid=${manid}`;
      this.axios.post(url, params).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          Toast.success('删除成功');
          setTimeout(() => {
            this.$router.back();
          }, 2000);
        }
      });
    },
    Go_back() {
      this.$router.go(-1);
    },
    selectlinkman() {
      const url = 'user/contact/select';
      let params = `manid=${this.$route.query.manid}`;
      this.axios.post(url, params).then(res => {
        console.log(res);
        this.editingContact = res.data.data[0];
      });
    },
  },
  mounted() {
    this.selectlinkman();
  },
};
</script>

<style lang="scss" scoped></style>
